#!/usr/bin/env python3
#
#  Copyright 2016 The TMM/AD Authors. All rights reserved.
#  Use of this source code is governed by a BSD-style license that can be
#  found in the license.txt file.
#

import tmm.web.base.encoding as _encoding


def read_text_file(path, encoding=_encoding.TMM_WEB_ENCODING):
    """Read a text file.

    :type path: str
    :type encoding: str
    :param path: The file path.
    :param encoding: The file encoding.
    :rtype : str | None
    :return: The file content on success. Otherwise, return None.
    """

    fp = None
    # noinspection PyBroadException
    try:
        fp = open(path, "r", encoding=encoding)
        content = fp.read()
        fp.close()

        return content
    except:
        #  Close the file if possible.
        if fp is not None and not fp.closed:
            fp.close()

        return None
